import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './modules/user/user.module';
import { DbModule } from './db/db.module';
import { DeptModule } from './modules/dept/dept.module';
import { MenuModule } from './modules/menu/menu.module';
import { RoleModule } from './modules/role/role.module';
import { AuthModule } from './modules/auth/auth.module';
import { GlobalModule } from './common/global.module';
import { TransformInterceptor } from './common/interceptors/transform.interceptor'
import { APP_FILTER, APP_INTERCEPTOR, APP_GUARD } from '@nestjs/core/constants';
import { AllExceptionsFilter } from './common/filters/any-exception.filter';
import { PermissionGuard } from './common/guards/permission.guard';
import { LoginLogModule } from './modules/login-log/login-log.module';
import { JwtAuthGuard } from './modules/auth/guards/jwt-auth.guard';
import { ConfigModule } from '@nestjs/config';
import config from '~/config'
import { SharedModule } from '~/shared/shared.module';
import { seconds, ThrottlerModule } from '@nestjs/throttler';
@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      expandVariables: true,
      // 指定多个 env 文件时，第一个优先级最高
      envFilePath: ['.env.local', `.env.${process.env.NODE_ENV}`, '.env'],
      load: [...Object.values(config)],
    }),
    ThrottlerModule.forRootAsync({
      useFactory: () => ({
        errorMessage: '当前操作过于频繁，请稍后再试！',
        throttlers: [
          { ttl: seconds(10), limit: 7 },
        ],
      }),

    }),
    SharedModule,
    DbModule,
    GlobalModule,
    UserModule,
    DeptModule,
    MenuModule,
    RoleModule,
    // PermissionModule,
    AuthModule,
    LoginLogModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    { provide: APP_FILTER, useClass: AllExceptionsFilter },
    { provide: APP_INTERCEPTOR, useClass: TransformInterceptor },
    { provide: APP_GUARD, useClass: JwtAuthGuard },
    { provide: APP_GUARD, useClass: PermissionGuard },
  ],
})
export class AppModule { }
